//
//  XNProgressHUDVC.m
//  ZSHUDSet-master
//
//  Created by safiri on 2018/7/2.
//  Copyright © 2018年 safiri. All rights reserved.
//

#import "XNProgressHUDVC.h"
#import "UIViewController+XNProgressHUD.h"

@interface XNProgressHUDVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic ,strong) NSArray *sectionTitles;

@property (nonatomic ,strong) NSArray *UIWindowHUDTitles;

@property (nonatomic ,strong) NSArray *UIViewControllerHUDTitles;

@end

@implementation XNProgressHUDVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置显示位置
    [XNHUD setPosition:CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height * 0.7)];
    // 设置主色调
    [XNHUD setTintColor:[UIColor colorWithRed:38/255.0 green:50/255.0 blue:56/255.0 alpha:0.8]];
    
    // 设置相应的maskType状态下的颜色（16进制颜色值）
    [XNHUD setMaskType:(XNProgressHUDMaskTypeBlack)  hexColor:0x00000044];
    [XNHUD setMaskType:(XNProgressHUDMaskTypeCustom) hexColor:0xff000044];
    
    //延时显示时间和延时消失时间，该方法只对下一次HUD显示生效（只生效一次）。
    //[XNHUD setDisposableDelayResponse:1.0f delayDismiss:2.0f];
    //设置排列方向，默认为水平方向
    //[XNHUD setOrientation:XNProgressHUDOrientationHorizontal];
    
    self.UIWindowHUDTitles = @[@"showLoading", @"showWithTitle",@"showInfo",@"showError",@"showSuccess",@"showProgress"];
    self.UIViewControllerHUDTitles = @[@"showLoading", @"showWithTitle",@"showInfo",@"showError",@"showSuccess"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.sectionTitles = @[@"在UIWindow上显示",@"在UIViewController上显示",@"在UIView上显示"];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.UIWindowHUDTitles.count;
    }else if (section == 1) {
        return self.UIViewControllerHUDTitles.count;
    }
    return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.sectionTitles[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.section == 0) {
        cell.textLabel.text = self.UIWindowHUDTitles[indexPath.row];
    }else if (indexPath.section == 1) {
        cell.textLabel.text = self.UIViewControllerHUDTitles[indexPath.row];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [XNHUD showLoadingWithTitle:@"正在登录"];
        }else if (indexPath.row == 1) {
            [XNHUD showWithTitle:@"这是一个支持自定义的轻量级HUD"];
        }else if (indexPath.row == 2) {
            [XNHUD showInfoWithTitle:@"邮箱地址不能为空"];
        }else if (indexPath.row == 3) {
            [XNHUD showErrorWithTitle:@"拒绝访问"];
        }else if (indexPath.row == 4) {
            [XNHUD showSuccessWithTitle:@"操作成功"];
        }else if (indexPath.row == 5) {
            __block CGFloat progress = 0;
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
            dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
            dispatch_source_set_event_handler(timer, ^{
                progress += 0.01;
                if (progress >= 1) { //倒计时结束，关闭
                    dispatch_source_cancel(timer);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [XNHUD showSuccessWithTitle:@"下载成功"];
                        [XNHUD dismiss];
                    });
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [XNHUD showProgressWithTitle:@"加载进度" progress:progress maskType:XNProgressHUDMaskTypeBlack];
                });
                
            });
            dispatch_resume(timer);
            
        }
    }
    else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            [self.hud showLoadingWithTitle:@"正在登录"];
        }else if (indexPath.row == 1) {
            [self.hud showWithTitle:@"这是一个支持自定义的轻量级HUD"];
        }else if (indexPath.row == 2) {
            [self.hud showInfoWithTitle:@"邮箱地址不能为空"];
        }else if (indexPath.row == 3) {
            [self.hud showErrorWithTitle:@"拒绝访问"];
        }else if (indexPath.row == 4) {
            [self.hud showSuccessWithTitle:@"操作成功"];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
